#include<iostream>
#include<cmath>
#include<cstdio>//zoj 2859
using namespace std;
const int maxv = 301;
//RMQ 2D
int val[maxv][maxv];
/*********二维RMQ模板******************/
int M,N;
const int maxn =301;
const int maxm = 301;
int rmq[maxn][maxm][9][9];//矩阵行是从1到N，列是从1到M，后面两维是RMQ的二进制
void RMQINIT2()//求最小模板
{
	int row,col,k,mx,my,i,j;
	for(row = 1; row <= N; row++)
	for(col = 1; col <=M; col++)
		rmq[row][col][0][0] = val[row][col];
	for(k=0;(1<<k)<=N;k++);
    	 mx=k-1;
    for(k=0;(1<<k)<=M;k++);
    	 my=k-1;
	for( i=0; i<= mx; i++)
	for( j = 0; j<=my; j++)
	{
		if(i == 0 && j ==0) continue;
		for( row = 1; row+(1<<i)-1 <= N; row++)
		 for(col = 1; col+(1<<j)-1 <= M; col++)
		 {
	       if(i == 0)//y轴二分
		   rmq[row][col][i][j]=
		min( rmq[row][col][i][j-1], rmq[row][col+(1<<(j-1))][i][j-1]);  
          else//x轴二分
           rmq[row][col][i][j] = min( rmq[row][col][i-1][j],  rmq[row+(1<<(i-1))][col][i-1][j]);
		 }
	}
}
int ASKRMQ2(int x1,int x2,int y1,int y2)
{
	int k,kx,ky,m1,m2,m3,m4;
	for(k=0;(1<<k)<=(x2-x1+1);k++);
	kx=k-1;
    for(k=0;(1<<k)<=(y2-y1+1);k++);
    ky=k-1;
    m1 = rmq[x1][y1][kx][ky];
    m2 = rmq[x2-(1<<kx)+1][y1][kx][ky];
    m3 = rmq[x1][y2-(1<<ky)+1][kx][ky];
    m4 = rmq[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];
    return min( min(m1,m2) , min(m3,m4));
}
/*求最大模板
void RMQINIT2()
{
	int row,col,k,mx,my,i,j;
	for(row = 1; row <= N; row++)
	for(col = 1; col <=M; col++)
		rmq[row][col][0][0] = val[row][col];
	for(k=0;(1<<k)<=N;k++);

    	 mx=k-1;
    for(k=0;(1<<k)<=M;k++);
    	 my=k-1;
	for( i=0; i<= mx; i++)
	for( j = 0; j<=my; j++)
	{
		if(i == 0 && j ==0) continue;
		for( row = 1; row+(1<<i)-1 <= N; row++)
		 for(col = 1; col+(1<<j)-1 <= M; col++)
		 {
	       if(i == 0)//y轴二分
		   rmq[row][col][i][j]=
		max( rmq[row][col][i][j-1], rmq[row][col+(1<<(j-1))][i][j-1]);  
          else//x轴二分
           rmq[row][col][i][j] = max( rmq[row][col][i-1][j],  rmq[row+(1<<(i-1))][col][i-1][j]);
		 }
	}
}
int ASKRMQ2(int x1,int x2,int y1,int y2)
{
	int k,kx,ky,m1,m2,m3,m4;
	for(k=0;(1<<k)<=(x2-x1+1);k++);
	kx=k-1;
    for(k=0;(1<<k)<=(y2-y1+1);k++);
    ky=k-1;
    m1 = rmq[x1][y1][kx][ky];
    m2 = rmq[x2-(1<<kx)+1][y1][kx][ky];
    m3 = rmq[x1][y2-(1<<ky)+1][kx][ky];
    m4 = rmq[x2-(1<<kx)+1][y2-(1<<ky)+1][kx][ky];
    return max( max(m1,m2) , max(m3,m4));
}
*/
/*********二维RMQ模板******************/
int  main()
{
         int Q,x1,y1,x2,y2,t,n;
         scanf("%d",&t);
         while(t--)  //N x M
         {
         	scanf("%d",&n);
         	N=n,M=n;
           for(int i=1; i<=N; i++)
           {
                for( int j=1; j<=M; j++)
                {         scanf("%d",&val[i][j]);
            	}
            }
            
                   RMQINIT2();
                   scanf("%d",&Q);
                   while(Q--)
                   {
                        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                        int x = ASKRMQ2(x1,x2,y1,y2);
                        printf("%d\n",x);
                   }
         }
         return 0;
}二维
